﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace _uitwerking__Algoritmiek
{
    public class code_GGD2
    {
        public code_GGD2()
        {
            //...
        }
        //Uitwerking GGD 2
        public int Execute(int M, int N)
        {
            if (M < N)
            {
                int H = M;
                M = N;
                N = H;
            }
            int rest = -1;
            while (rest != 0)
            {
                rest = M % N;
                M = N;
                N = rest;
            }
            return M;
            //List<int> Mprimes = Eratosthenes(M);
            //List<int> Nprimes = Eratosthenes(N);

            //for (int i = Mprimes.Last(); i >= 0; i--)
            //{
            //    for (int j = Nprimes.Last(); j >= 0; i--)
            //    {
            //        if (i == j)
            //        {
            //            return i;
            //        }
            //    }
            //}
        }

        #region Zeef van Eratosthenes
        List<int> primes;

        public List<int> Eratosthenes(int max)
        {
            primes = new List<int>();
            primes.Add(2);
            for (int i = 3; i < max; i += 2)
            {
                if (IsPrime(i))
                {
                    primes.Add(i);
                }
            } return primes;
        }


        public bool IsPrime(int i)
        {
            bool isprime = true;

            foreach (int prime in primes)
            {
                if ((i % prime) == 0 && prime <= Math.Sqrt(i))
                {
                    isprime = false;
                    break;
                }
            }

            return isprime;
        }

        #endregion
    }
}
